﻿#define _CRT_SECURE_NO_WARNINGS

//包含r 49add.h的头文件，所有的信息可以都放在49add.h中，以便统一
#include "game.h"

//打印游戏控制台页面的函数
void print_game()
{
	printf("**************************\n");
	printf("********   1.Play   ******\n");
	printf("********   2.Exit   ******\n");
	printf("**************************\n");

}

//定义一个初始化数组的函数，初始化的值由set来决定
void initboard(char board[ROWS][COLS], int row, int col, char set)
{

	int i = 0;
	int j = 0;
	for (i = 0;i < row;i++)
	{
		for (j = 0;j < col;j++)
		{
			board[i][j] = set;//
		}
	}
}

//打印棋盘的函数
void displayboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("--------扫雷--------\n");
	for (i = 0;i <= col;i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1;i <= row;i++)
	{
		printf("%d ", i);
		for (j = 1;j <= col;j++)
		{
			printf("%c ", board[i][j]);

		}
		printf("\n");
	}
	printf("--------扫雷--------\n");


}

//布置雷的函数
void setmine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;//给该棋盘布置10个雷，如果布置完，循环就结束
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}
}


//排查该坐标周围的8个坐标是否有雷
int getminecount(char mine[ROWS][COLS], int x, int y)
{
	return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1]) - 8 * '0';
}

//排查雷
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - EASY_COUNT)
	{
		printf("请输入要查找的坐标\n");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				displayboard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = getminecount(mine, x, y);
				show[x][y] = count + '0';
				displayboard(show, ROW, COL);
				win++;

			}
		}
		else
		{
			printf("你输入的坐标有误，请重新输入\n");
		}


	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你，排雷成功！\n");
		displayboard(mine, ROW, COL);
	}
}

void game()
{
	//定义二个二维数组
	//mine⽤来存放布置好的雷的信息
	char mine[ROWS][COLS] = { 0 };
	//show⽤来存放排查出的雷的个数信息
	char show[ROWS][COLS] = { 0 };

	//初始化数组调用函数
	initboard(mine, ROWS, COLS, '0');
	initboard(show, ROWS, COLS, '*');

	//打印棋盘
	//displayboard(mine, ROW, COL);
	//displayboard(show, ROW, COL);

	//布置雷
	setmine(mine,ROW,COL);
	displayboard(show, ROW, COL);

	//用打印棋盘的函数来看一个布置雷的信息
	//displayboard(mine, ROW, COL);

	//排查雷
	findmine(mine, show, ROW, COL);


}